<?php

function init() {
    if (Settings::$showErrors) {
        error_reporting(E_ALL);
    }
    if (isset($_GET["lang"])) {
        $lang = addslashes($_GET["lang"]);
        setLang($lang);
        setcookie("lang", $lang, time() + 60 * 60 * 24 * 30, "/");
    } else {
        if (isset($_COOKIE['lang'])) {
            setLang(addslashes($_COOKIE['lang']));
        } else {
            setLang(Settings::$defaultLang);
        }
    }
    $GLOBALS["currentYear"] = Date("Y");
    SQL::init();
    connect();
    UserFunc::checkUser();
    InitTemplates::creaTemplateManifest();
}

final class UserFunc {

    public static function checkUser() {
        session_start();
        global $user;
        if (isset($_SESSION['SESS_MEMBER_ID'])) {
            $user = $_SESSION['SESS_MEMBER_ID'];
            
        } else {
            $user = new User();
            $_SESSION['SESS_MEMBER_ID'] = $user;
        }
        session_write_close();
    }

    public static function login($login, $pass) {
        $result = selectUser($login, $pass);
        $loged = false;
        while ($result && $row = mysql_fetch_assoc($result)) {            
            $user = getUser();
            $user->setUserId($row['id']);
            $user->setRole($row['role']);
            $user->setEmail($row['login']);
            session_start();
            $_SESSION['SESS_MEMBER_ID'] = $user;            
            session_write_close();            
            $loged = true;
        }
        return $loged;
    }

}

final class InitTemplates {

    public static function creaTemplateManifest() {
        
        $result = selectManifest();
        $initMani = true;
        $i = 0;
        if ($result != 0) {
            while ($row = mysql_fetch_array($result)) {
                $initMani = false;
                $array[$i][0] = $row["templateName"];
                $array[$i][1] = $row["templatePath"];
                $array[$i][2] = $row["role"];
                $i++;
            }
        }
        if (isset($array))
            $GLOBALS["templateManifest"] = $array;

        if ($initMani) {            
            createManifest();
            foreach (glob("templates/*.html") as $filename) {
                $name = explode("/", $filename);
                $name = explode(".", $name[1]);
                $name = $name[0];
                insertManifest($name, $filename, Settings::$roleNoneString);
            }
            foreach (glob("customTemplates/*.html") as $filename) {
                $name = explode("/", $filename);
                $name = explode(".", $name[1]);
                $name = $name[0];
                insertManifest($name, $filename, Settings::$roleNoneString);
            }
            foreach (glob("customTemplates/fragments/*.html") as $filename) {
                $name = explode("/", $filename);
                $name = explode(".", $name[2]);
                $name = $name[0];
                insertManifest($name, $filename, Settings::$roleJornString);
            }
            foreach (glob("customTemplates/admin/*.html") as $filename) {
                $name = explode("/", $filename);
                $name = explode(".", $name[2]);
                $name = $name[0];
                insertManifest($name, $filename, Settings::$roleJornString);
            }
            InitTemplates::creaTemplateManifest();
        }
    }

}

function finally() {
    if (Settings::$debug && getStackTrace() != null) {
        //createLog(EventMsg::$FINALIZACE, getStackTrace());
    }
    close();
}

?>
